//
//  XMConversationToolsView.m
//  XunMi
//
//  Created by MacBook on 2024/4/10.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMConversationToolsView.h"

@interface XMConversationToolsView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray <XMConversationToolsModel *> *dataArray;
@end

@implementation XMConversationToolsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray array];
        NSArray *array = @[@"守护", @"视频", @"礼物", @"语音", @"开盲盒"];
        [array enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            XMConversationToolsModel *model = [[XMConversationToolsModel alloc] initWithName:obj img:kStringFormat(@"con_tools_%ld", idx + 1) type:idx + 1];
            [self.dataArray addObject:model];
        }];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(kScreenWidth / array.count, 56);
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self addSubview:self.collectionView];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self.collectionView registerClass:XMConversationToolsCell.class forCellWithReuseIdentifier:kStringFromClass(XMConversationToolsCell)];
        
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMConversationToolsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMConversationToolsCell) forIndexPath:indexPath];
    XMConversationToolsModel *model = [self.dataArray safe_objectAtIndex:indexPath.item];
    cell.model = model;
    XMWeakSelf(self);
    cell.clickBlock = ^(XMConversationToolsModel * _Nonnull model) {
        XMStrongSelf(self);
        if (self.clickBlock) {
            self.clickBlock(model.type);
        }
    };
    return cell;
}

@end


@implementation XMConversationToolsModel

- (instancetype)initWithName:(NSString *)name img:(NSString *)img type:(XMConversationToolsType)type {
    if (self = [super init]) {
        self.name = name;
        self.img = img;
        self.type = type;
    }
    return self;
}

@end


@implementation XMConversationToolsCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_button];
        [_button setTitleColor:kHEX(@"#959595") forState:UIControlStateNormal];
        _button.titleLabel.font = kUIFont(12);
        [_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        _button.adjustsImageWhenHighlighted = NO;

    }
    return self;
}

- (void)setModel:(XMConversationToolsModel *)model {
    _model = model;
    [_button setImage:kGetImage(model.img) forState:UIControlStateNormal];
    [_button setTitle:model.name forState:UIControlStateNormal];
}

- (void)click:(UIButton *)sender {
    if (self.clickBlock) {
        self.clickBlock(self.model);
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.button setImagePosition:XMImagePositionTop spacing:1];
}

@end


